<?php

function cable_category_load($connector)
{
	return true;
}

function cable_category_unload($connector)
{
	return true;
}

function cable_category_open(&$connector)
{
	if ($connector->data[cached])
		return true;
		
	if (($_REQUEST['category'] == '') && ($_REQUEST['c'] == ''))
		return true;
	
	/* get the name of the category, strip out nasties */
	if ($_REQUEST['category'])
		$category = str_replace('"','',str_replace("'",'',urldecode($_REQUEST['category'])));
	else
		$category = str_replace('"','',str_replace("'",'',urldecode($_REQUEST['c'])));
	
	/* mark for deletion all entries not in the correct category */
	$counter = 0;
	$last_timestamp = time();
	foreach ($connector->data as $timestamp => $data)
	{
		if (in_array($category,$data['category']))
		{
			$counter++;
			$last_timestamp = $timestamp;
		}
		else
			$connector->data[$timestamp][delete] = true;
	}

	/* if there's not enough entries, get some more */
	if ($counter < $connector->config['IndexEntries'])
	{
		/* sort so that most recent entries are first */
		if ($connector->config['SearchSort'] != 'descending')
			krsort($connector->data[$row[timestamp]]);
		
		/* call the recursive function to append more entries to our data array */
		$ret = cable_categoryGetEntries($connector,$last_timestamp,$category);
	
		/* re-apply correct sorting */
		if ($connector->config['SearchSort'] != 'descending')
			ksort($connector->data[$row[timestamp]]);
	
		/* if there was an error, return it */
		if ($ret !== true)
			return $ret;
	}
	
	/* now see if there's any appropriate child templates to switch to */
	if (($category_template = $connector->template->GetChild('CABLE-'.ucfirst(strtolower($category)))) !== false)
		$connector->template = $category_template;

	return true;
}

function cable_category(&$connector)
{
	/* empty stub */

	return true;
}

function cable_category_close(&$connector)
{
	/* empty stub */

	return true;
}

function cable_categoryGetEntries(&$connector,$last_timestamp,$category)
{
	$table = "cable_".$connector->config['TablePrefix']."_entries";
	$query = "SELECT timestamp,author,title,data FROM $table WHERE data LIKE '%".$category."%' AND timestamp < $last_timestamp ORDER BY timestamp DESC LIMIT ".$connector->config['IndexEntries'];
	if (($result = mysql_query($query,$connector->db_link)) === false)
		return 'Error accessing entry table';

	/* load entry data results into array */
	while ($row = mysql_fetch_assoc($result))
	{
		/* extract serialized entry data */
		$data = unserialize($row[data]);
		unset($row[data]);
		foreach($data as $key => $value)
		{
			if (!isset($entry_array[$key]))
			{
				if (is_string($value))
					$entry_array[$key] = rawurldecode($value);
				else
					$entry_array[$key] = $value;
			}
		}
		
		/* append to connector data array if the category array contains the right category */
		if (in_array($category,$data['category']))
			$connector->data[$row[timestamp]] = array_merge($data,$row);
	}
		
	/* see if we have the requisite number of entries yet */
	if (mysql_num_rows($result) > 0) /* endless loop check */
	{
		$counter = 0;
		$last_timestamp = time();
		
		/* count the number of appropriate entries */
		foreach ($connector->data as $timestamp => $data)
		{
			if (!$data[delete])
			{
				$counter++;
				$last_timestamp = $timestamp;
			}
		}
		
		/* if we're still short, run a recursive call to get more */
		if ($counter < $connector->config['IndexEntries'])
			return cable_categoryGetEntries($connector,$last_timestamp,$category);
	}
				
	return true;
}

?>